<template>
  <el-form
    ref="formRef"
    :model="formData"
    :rules="formRules"
    v-loading="formLoading"
    label-width="0px"
    :inline-message="true"
  >
    <el-table :data="formData" class="-mt-10px">
      <el-table-column label="序号" type="index" width="100" />
      

      <el-table-column label="文件内容"  prop="file" width="110px">
        <template #default="{ row }">
          <el-image
            v-if="row.fileName.includes('jpg')||row.fileName.includes('image')||row.fileName.includes('png')"
            class="h-80px w-80px"
            lazy
            :src="row.file"
            :preview-src-list="[row.file]"
            preview-teleported
            fit="cover"
          />
          <el-link
            v-else-if="row.fileName.includes('pdf')"
            type="primary"
            :href="row.file"
            :underline="false"
            target="_blank"
            >预览</el-link
          >
          <el-link v-else type="primary" download :href="row.file" :underline="false" target="_blank"
            >下载</el-link
          >
        </template>
      </el-table-column>

      <!-- <el-table-column label="创建者" min-width="150">
        <template #default="{ row}">
          <div>
            <el-link :href="row.file" target="_blank">点击下载文件</el-link>
          </div>

        </template>
      </el-table-column> -->




    </el-table>
  </el-form>
  <!-- <el-row justify="center" class="mt-3">
    <el-button @click="handleAdd" round>+ 添加投诉文件</el-button>
  </el-row> -->
</template>
<script setup lang="ts">
import { ReportingApi } from '@/api/system/reporting'

const props = defineProps<{
  reportingId: undefined // 投诉id（主表的关联字段）
}>()
const formLoading = ref(false) // 表单的加载中
const formData = ref([])
const formRules = reactive({
})
const formRef = ref() // 表单 Ref

/** 监听主表的关联字段的变化，加载对应的子表数据 */
watch(
  () => props.reportingId,
  async (val) => {
    // 1. 重置表单
    formData.value = []
    // 2. val 非空，则加载数据
    if (!val) {
      return;
    }
    try {
      formLoading.value = true
      const data = await ReportingApi.getReportingFileListByReportingId(val)
      // data.forEach(element => {
      //   data.fileName = element.fileName.split('.').pop(); 
      //   formData.value = data
      //   console.log("111111111",formData.value)
      // });
      for(let i =0;i<data.length;i++){
        data[i].fileName = data[i].fileName.split('.').pop(); 
        formData.value = data
      }
      console.log(formData.value)
    } finally {
      formLoading.value = false
    }
  },
  { immediate: true }
)

/** 新增按钮操作 */
const handleAdd = () => {
  const row = {
    id: undefined,
    reportingId: undefined,
    file: undefined,
    create: undefined,
    update: undefined,
  }
  row.reportingId = props.reportingId
  formData.value.push(row)
}

/** 删除按钮操作 */
const handleDelete = (index) => {
  formData.value.splice(index, 1)
}

/** 表单校验 */
const validate = () => {
  return formRef.value.validate()
}

/** 表单值 */
const getData = () => {
  return formData.value
}

defineExpose({ validate, getData })
</script>